import { _decorator, Component, director, Node, Prefab, Tween, tween, v3, Vec3, view } from 'cc';
import Banner from '../Banner';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
import { NodeUtil } from '../AshFramework/Utils/NodeUtil';
import { Panel, UIManager } from '../AshFramework/Managers/UIManager';
import { GameManager } from '../GameManager';
import { Item } from './Item';
import { PoolManager } from '../AshFramework/Managers/PoolManager';
import { ItemData } from '../Data';
import { EventManager, MyEvent } from '../AshFramework/Managers/EventManager';
import UI_Shaking from '../AshFramework/UI/UI_Shaking';
import { DataManager } from '../AshFramework/Managers/DataManager';

const { ccclass, property } = _decorator;

@ccclass('DeckPanel')
export class DeckPanel extends Component {

    Panel: Node;
    Content: Node;
    ShowReplace: Node;
    ShowReplaceContent: Node;

    items: Node[] = [];

    replaceItemData: ItemData;

    protected onLoad(): void {
        this.Panel = NodeUtil.GetNode("Panel", this.node);
        this.Content = NodeUtil.GetNode("Content", this.node);
        this.ShowReplace = NodeUtil.GetNode("ShowReplace", this.node);
        this.ShowReplaceContent = NodeUtil.GetNode("ShowReplaceContent", this.node);
    }

    Show(data: ItemData = null, resetPosition: boolean = true) {
        if (Banner.NeedShowMoreAd) {
            Banner.Instance.ShowBannerAd();
            Banner.Instance.ShowCustomAd();
        }

        this.replaceItemData = data;
        this.ShowReplace.active = data != null;

        if (data) {
            this.ShowReplaceContent.children.forEach(e => PoolManager.PutNode(e));

            PoolManager.GetNode("Prefabs/UI/Item", this.ShowReplaceContent).then((item: Node) => {
                item.getComponent(Item).InitSimple(data);
            });
        }

        this.RefreshCards();

        if (resetPosition) this.Panel.setPosition(0, -view.getVisibleSize().height / 2 - 1125 / 2, 0);
        Tween.stopAllByTarget(this.Panel);

        tween(this.Panel).to(0.5, { position: v3(0, data == null ? 100 : 400, 0) }, { easing: `backOut` }).call(() => { }).start();
    }

    RefreshCards() {
        this.items.forEach(e => PoolManager.PutNode(e));

        for (let i = 0; i < DataManager.equippedItemDatas.length; i++) {
            const data = DataManager.equippedItemDatas[i];

            PoolManager.GetNode("Prefabs/UI/Item", this.Content).then((item: Node) => {
                item.setRotationFromEuler(Vec3.ZERO);

                if (this.replaceItemData) {
                    item.getComponent(Item).InitWithCallback(data, this.Replace.bind(this));
                    item.getComponent(UI_Shaking).Shaking(15, 2.5);
                } else {
                    item.getComponent(Item).Init(data);
                    item.getComponent(UI_Shaking).Stop();
                }

                this.items.push(item);
            });
        }

    }

    //要替换的卡牌
    Replace(data: ItemData) {
        let index = DataManager.equippedItemDatas.findIndex(e => e == data);
        if (index != -1) {
            DataManager.equippedItemDatas[index] = this.replaceItemData;
            DataManager.SaveData();
            EventManager.Scene.emit(MyEvent.REFRESH_SHOPITEMS);
            this.replaceItemData = null;
            this.Show(null, false);
        }
    }

    OnButtonClick(event, data) {
        AudioManager.PlaySound(Audios.ButtonClick);

        switch (event.target.name) {
            case 'CloseButton':
                UIManager.GetInstance().HidePanel(Panel.DeckPanel);
                break;
        }
    }

}